/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import info.peigen.hotpot.manage.core.domain.Organization;
import info.peigen.hotpot.manage.core.manage.convertor.OrganizationManageConvertor;
import info.peigen.hotpot.manage.core.manage.repository.mapper.OrganizationManageMapper;
import info.peigen.hotpot.manage.core.repository.entity.OrganizationEntity;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(OrganizationManageRepository)</b>
 * 管理后台 "组织机构" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-02
 */
@SuppressWarnings("unused")
@Component
public class OrganizationManageRepository extends AbstractBeetlSqlRepository<OrganizationManageMapper, OrganizationEntity> {
    public Organization active(String organizationId) {
        Assert.isTrue(StrUtil.isNotEmpty(organizationId), () -> new BusinessException(ResultCode.FAILURE, "激活时[organizationId]不得为空!"));
        OrganizationEntity entity = getMapper().findByOrganizationId(organizationId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!organizationId=" + organizationId));
        return OrganizationManageConvertor.INSTANCE.convertToDomain(entity);
    }

    public Organization lock(String organizationId) {
        Assert.isTrue(StrUtil.isNotEmpty(organizationId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[organizationId]不得为空!"));
        OrganizationEntity entity = getMapper().lockByOrganizationId(organizationId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!organizationId为空!"));
        return OrganizationManageConvertor.INSTANCE.convertToDomain(entity);
    }

    public void removeByOrganizationId(String organizationId) {
        Assert.isTrue(StrUtil.isNotEmpty(organizationId), () -> new BusinessException(ResultCode.FAILURE, "删除时[organizationId]不得为空!"));
        getMapper().deleteByOrganizationId(organizationId);
    }

    public void removeByIds(List<Integer> ids) {
        Assert.isTrue(CollUtil.isNotEmpty(ids), () -> new BusinessException(ResultCode.FAILURE, "删除时[id]不得为空!"));
        getMapper().deleteByIds(ids);
    }

    public void logicDeleteById(Integer id) {
        delete(id);
    }

    public int logicDeleteByOrganizationId(String organizationId) {
        Assert.isTrue(StrUtil.isNotEmpty(organizationId), () -> new BusinessException(ResultCode.FAILURE, "逻辑删除时[organizationId]不得为空!"));
        return getMapper().createLambdaQuery().andEq(OrganizationEntity::getOrganizationId, organizationId).delete();
    }

    public void store(Organization domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));
        OrganizationEntity entity = OrganizationManageConvertor.INSTANCE.convertToEntity(domain);
        saveOrUpdate(entity);
    }

}